<?php
/**
 * Created by PhpStorm.
 * User: xiaojun
 * Date: 15/12/22
 * Time: 下午3:08
 */

namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class NewsController extends BaseController {


    public function index(){
        if(!IS_GET){
            $this->error('非法访问');
        }
        $this->display('index');
    }

    public function new_list(){
        if(!IS_GET){
            $this->error('非法访问');
        }
        //获取id=1页的信息
        $par['classify'] = intval(I('get.id'));
        $access_type = I('get.type');
        if(empty($par['classify'])){
            $par['classify'] = 1;
        }
        $par['id'] = 3;
        $par['p'] = intval(I('get.p'));
        $par['rows'] = 15;
        if(empty($par['p'])){
            $par['p'] = 1;
        }
        $news_info = json_decode($this->http->curl_request(C('TIDOG_GATEGORY_HOST').http_build_query($par)),true);
        $list_news = [];
        if($news_info){
            $list_news = $news_info['list'];
        }
        $category_info = F('index_category');
        if($access_type === 'xhr'){
            $this->ajaxReturn(['list_news'=>$list_news,'category'=>$category_info[$par['classify']]['title'],
                               'category_class'=>$category_info[$par['classify']]['class'],'code'=>200]);
            exit();
        }
        $this->assign('list_news',$list_news);
        $this->assign('category',$category_info);
        $this->assign('tngou_image_prefix',C('TIDOG_IMG_PREFIX'));
        $this->display('list');
    }

    function detail() {
        if(!IS_GET){
            $this->error('非法访问');
        }
        $id = intval(I('get.id'));
        if(empty($id)){
            $this->error('未知的资讯请求');
        }
        $info = json_decode($this->http->curl_request(C('TIDOG_NEWS_SHOW').$id),true);
        if($info){
            $this->assign('info',$info);
            $this->assign('tngou_image_prefix',C('TIDOG_IMG_PREFIX'));
            $category_info = F('index_category');
            $this->assign('category',$category_info);
        }else{
            $this->error('未找到该详情');
        }
        $this->display('detail');
    }

}